const app = getApp()
Page({
  data: {
    page: {
      currentPage: 1,
      pageSize: 10,
    },
    total: 0,
    currentTab: 0,
    courseLibraryList: [],
    tabList:[],
    tempcourseName:'',
    courseName:'',
    contentResIds:[],
    contentCN:[],
    phoneType:app.globalData.phoneType
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    let contentResIds = app.globalData.contentResIds||[]
    let contentCN = app.globalData.contentCN||[]
    this.setData({
      contentResIds:contentResIds,
      contentCN:contentCN
    })
    this.getCourseLibraryList()
    this.getCourseType()
  },
  onShow: function () {
    // let arr = this.datacourseLibraryList
    // let contentResIds = this.data.contentResIds
    // if(contentResIds.length>0){
    //   for (let i = 0; i < contentResIds.length; i++) {
    //     let id = contentResIds[i]
    //     arr.forEach(ele=>{
    //       ele.id===id && (ele.checked=true)
    //     })
    //   }
    // }
  },
  searchByname(){
    this.choseSave()
    this.setData({
      currentTab: 0,
      courseName:this.data.tempcourseName
    })
    this.getCourseLibraryList()
    console.log('123')
  },
  getCourseType () {
    this.choseSave()
    app.api.post({
      url: 'vx_study_courseLibrary_list',
    }).then(res => {
      console.log(res)
      let list = [{id:0,courseLibraryName:'全部'}]
      res.length>0 && list.push(...res)
      this.setData({
        tabList:list||[]
      })
    })
  },
  getCourseLibraryList () {
    const { currentPage, pageSize } = this.data.page
    let { courseLibraryList } = this.data
    let data = {
      lv1CourseLibraryId:this.data.currentTab===0?'':this.data.currentTab,
      courseName:this.data.courseName,
      currentPage,
      pageSize,
    }
    app.api.post({
      url: 'vx_study_course_listByLv1LibIdAndCourseName',
      data
    }).then(res => {
      console.log(res)
      let list = res.list||[]
      for (let i = 0; i < list.length; i++) {
        let arr_chose = this.data.contentResIds
        list[i].checked = false
        // console.log(list[i].id.indexOf(arr_chose))
        if(arr_chose.length>0){
          console.log(arr_chose.includes(list[i].id),list[i].id,arr_chose[0]) 
          // console.log(list[i].id)
          arr_chose.includes(list[i].id) && (list[i].checked = true)
        }
        
      }
      currentPage === 1 ? (courseLibraryList = list) : courseLibraryList.push(...list)
      this.setData({
        courseLibraryList,
        total: res.total
      })
    })
  },
  //点击切换
  clickTab: function (e) {
    this.choseSave()
    console.log(e)
    let that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return ;
    } else {
      that.setData({
        currentTab: e.target.dataset.current,
        'page.currentPage': 1
      })
      this.getCourseLibraryList()
    }
  },
  checkedTap: function (e) {
    console.log(e)
    let id = e.currentTarget.dataset.id
    let courseLibraryList = this.data.courseLibraryList
    courseLibraryList.forEach((item)=>{
      item.id === id && (item.checked = !item.checked)
    })
    console.log(courseLibraryList)
    this.setData({
      "courseLibraryList": courseLibraryList
    })
  },
  //保存选择的radio
  choseSave(){
    let arr = this.data.courseLibraryList
    let arr_chose = this.data.contentResIds
    let arr_cn = this.data.contentCN
    console.log(this.data.contentResIds)
    for (let i = 0; i < arr.length; i++) {
      if(arr[i].checked===true){
        arr_chose.indexOf(arr[i].id)===-1 && arr_chose.push(arr[i].id) &&
        arr_cn.push(arr[i].courseName)  
      }
    }
    this.setData({
      contentResIds:arr_chose,
      contentCN: arr_cn
    })
  },
  //确定按钮
  courseSubmit(){
    this.choseSave()
    app.globalData.contentResIds = this.data.contentResIds
    app.globalData.contentCN = this.data.contentCN
    app.globalData.dataUpdata = 'pending'
    console.log(app.globalData)
    wx.navigateBack()
  },
  loadMore () {
    const { total, courseLibraryList, page } = this.data
    const { currentPage } = page
    if (total > courseLibraryList.length) {
      this.setData({
        'page.currentPage': currentPage + 1
      })
      this.getCourseLibraryList()
    }
  }
})
